<?
    require_once("config.php");
    
    $menu = new Menu("Message Administration");
    $menu->AddItem(new MenuItem("Message List","admin_messages.php",AuthConstants::ADMINISTRATORS));
    $menu->AddItem(new MenuItem("Create Message","admin_messages.php?action=new",AuthConstants::ADMINISTRATORS));
    
    if ($_POST) {
        $build = array(
            'message'   => $_POST['message'],
            'severity'  => $_POST['severity'],
            'createdon' => time(),
            'displayuntil' => time() + (60*60*24*$_POST['displayfor'])
        );
        
        $query = QueryTools::CreateQuery($build,'messages');
        echo $query . "<br>";
        DBM::Query($query);
        
        header("Location: admin_messages.php");
    }
    else if ($_GET['action'] == "delete") {
        DBM::Query("DELETE FROM messages WHERE id=$_GET[id];");
        header("Location: admin_messages.php");
    }
    else if ($_GET['action'] == "new") {
        MenuManager::AddMenu("Create Message",$menu);
        Functions::PrintHeader("Create Message");
        ?>
        <form method="POST" action="admin_messages.php">
            <table class='cgltable' align='center'>
            <thead>
                <tr>
                    <th colspan='2'>Create Message</th>
                </tr>
                </thead>
                <tr>
                    <td class='label'>Message<br><span style='font-size:8pt;'>Must limit to 255 characters</span></td>
                    <td><textarea name='message' class='wide' rows='5'></textarea></td>
                </tr>
                <tr>
                    <td class='label'>Display For</td>
                    <td><input type='text' name='displayfor' value='3' />days</td>
                </tr>
                <tr>
                    <td class='label'>Select Type</td>
                    <td>
                        <select name='severity'>
                            <option value='success'>Success Message</option>
                            <option value='error'>Error Message</option>
                            <option value='warning'>Warning Message</option>
                            <option value='info'>Information Message</option>
                        </select>
                        <hr class='wide' />
                        <b>Examples:</b><br>
                        <ul class='messagelist'>
                            <li class='success'>Sample success message</li>
                            <li class='error'>Sample error message</li>
                            <li class='warning'>Sample warning message</li>
                            <li class='info'>Sample info message</li>
                        </ul>
                    </td>
                </tr>
                <tfoot>
                <tr>
                    <td class='label' colspan='2'>
                        <input type='reset' value='Start Over' />
                        <input type='submit' value='Create Message' />
                    </td>
                </tr>
                </tfoot>
            </table>
        </form>
        <?
        Functions::PrintFooter();
    }
    else {
        MenuManager::AddMenu("Message Administration",$menu);
        Functions::PrintHeader("Message Administration");
        
        $messages = MessageManager::GetMessages();
        if ($messages) {
        ?>
        <table class='wide cgltable'>
        <thead>
            <tr>
                <th>ID</th>
                <th>Message</th>
                <th>Severity</th>
                <th>Created On</th>
                <th>Displayed Until</th>
                <th>Status</th>
                <th>&nbsp;</th>
            </tr>
        </thead>
            <?
                // get all messages that have been displayed in the past week
                foreach ($messages as $message) {
                    echo "<tr>
                            <td>$message[id]</td>
                            <td>$message[message]</td>
                            <td class='$message[severity] fadedbg' style='height:20px;'>$message[severity]</td>
                            <td>" . date("M d, Y g:i A",$message['createdon']) . "</td>
                            <td>" . date("M d, Y g:i A",$message['displayuntil']) . "</td>
                            <td>" . ($message['displayuntil'] < time() ? "Expired" : "Displayed") . "</td>
                            <td><a href='admin_messages.php?action=delete&id=$message[id]'>delete</a></td>
                        </tr>";
                }
            ?>
        </table>
        <?
        }
        else InfoMessage::Show("No Messages","There are no messages at this time.");
        Functions::PrintFooter();
    }
?>
